package com.cucn.ingmall.order.config;

import com.cucn.common.constant.order.OrderRabbitMqConstant;
import com.cucn.common.constant.seckill.SeckillConstant;
import com.cucn.common.constant.ware.WareConstant;
import com.cucn.ingmall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Rabbitmq配置
 *
 * @author KouChaoJie
 * @since: 2022/1/25 15:03
 */
@Configuration
@Slf4j
public class MyMQConfig {

    @Bean
    public Queue orderDelayQueue() {
        /*
        x-dead-letter-exchange: order-event-exchange
        x-dead-letter-routing-key: order.release.order
        x-message-ttl: 60000
         */
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", OrderRabbitMqConstant.ORDER_EVENT_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", OrderRabbitMqConstant.ORDER_RELEASE_ORDER_ROUTING_KEY);
        arguments.put("x-message-ttl", OrderRabbitMqConstant.X_MESSAGE_TTL);

        return new Queue(OrderRabbitMqConstant.ORDER_DELAY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Queue orderReleaseQueue() {
        return new Queue(OrderRabbitMqConstant.ORDER_RELEASE_ORDER_QUEUE, true, false, false);
    }

    /**
     * 秒杀服务队列
     *
     * @return
     */
    @Bean
    public Queue orderSeckillQueue() {
        return new Queue(SeckillConstant.ORDER_SECKILL_QUEUE, true, false, false);
    }


    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(OrderRabbitMqConstant.ORDER_EVENT_EXCHANGE, true, false);
    }

    @Bean
    public Binding orderSeckillOrderBinding() {
        return new Binding(SeckillConstant.ORDER_SECKILL_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMqConstant.ORDER_EVENT_EXCHANGE,
                SeckillConstant.ORDER_SECKILL_ORDER_ROUTING_KEY,
                null
        );
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding(OrderRabbitMqConstant.ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMqConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMqConstant.ORDER_CREATE_ORDER_ROUTING_KEY,
                null
        );
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(OrderRabbitMqConstant.ORDER_RELEASE_ORDER_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMqConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMqConstant.ORDER_RELEASE_ORDER_ROUTING_KEY,
                null
        );
    }

    /**
     * 订单释放直接和库存释放进行绑定
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding(WareConstant.STOCK_RELEASE_STOCK_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMqConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMqConstant.ORDER_RELEASE_OTHER_ROUTING_KEY,
                null
        );
    }
}